package com.swxc.smartorder.adapter;

import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.swxc.smartorder.R;
import com.swxc.smartorder.entity.ContactEntity;
import com.swxc.smartorder.entity.ContactSection;
import com.swxc.smartorder.inter.IndexAdapter;
import com.swxc.smartorder.inter.Indexable;
import com.swxc.smartorder.utils.ImageDisplayUtil;
import com.swxc.smartorder.widget.RoundImageView;

import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author   Administrator
 * Date     2018/3/30
 * Desc     eg
 *
 * @author Administrator
 */
public class AdapterContact extends BaseSectionQuickAdapter<ContactSection, BaseViewHolder> implements IndexAdapter {
    private Map<Integer, Boolean> map = new HashMap<>();
    private boolean onBind;
    private int checkedPosition = -1;


    public AdapterContact(List<ContactSection> data) {
        super(R.layout.item_contact, R.layout.item_contact_header, data);
    }


    /**
     * 获取当前选中位置
     *
     * @return
     */
    public int getCheckedPosition() {
        return checkedPosition;
    }


    @Override
    protected void convertHead(BaseViewHolder helper, ContactSection item) {
        helper.setText(R.id.tv_section, item.getHeaderName());

    }

    @Override
    protected void convert(BaseViewHolder helper, ContactSection item) {
        ContactEntity bean = item.t;
        final int position = helper.getLayoutPosition();
        helper.setText(R.id.tv_name, bean.getStaffName());
        helper.setText(R.id.tv_phone, bean.getStaffPhone());
        helper.setText(R.id.tv_zone, bean.getOrgName());
        RoundImageView riv = helper.getView(R.id.riv_avatar);

        CheckBox mCheckBox = helper.getView(R.id.checkBox);
        if (bean.getAvatar() != null) {
            ImageDisplayUtil.showImageView(mContext, bean.getAvatar(), riv);
        }
        helper.setOnCheckedChangeListener(R.id.checkBox, new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    map.clear();
                    map.put(position, true);
                    checkedPosition = position;
                } else {
                    map.remove(position);
                    if (map.size() == 0) {
                        //-1 代表一个都未选择
                        checkedPosition = -1;
                    }
                }
                if (!onBind) {
                    notifyDataSetChanged();
                }
            }

        });

        onBind = true;
        if (map != null && map.containsKey(position)) {
            mCheckBox.setChecked(true);
        } else {
            mCheckBox.setChecked(false);
        }
        onBind = false;


    }

    @Override
    public Indexable getItemdex(int position) {
        return null;
    }

    public int getLetterPosition(@Nullable String letter) {

        int position = -1;
        for (int i = 0; i < mData.size(); i++) {

            ContactSection item = mData.get(i);
            if (item.isHeader()) {
                if (item.header.equals(letter)) {
                    position = i;
                    break;
                }
            }
        }

        return position;

    }
}
